package com.sunday.fangdaifu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayout;
import com.squareup.picasso.Picasso;
import com.sunday.common.utils.PixUtils;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.model.HistoryTaskDetail;
import com.sunday.fangdaifu.widgets.ShowBigImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.sunday.fangdaifu.R.id.view;

/**
 * Created by yun_sheng on 2017/6/29.
 */

public class HistoryDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<HistoryTaskDetail> dataSet;
    private View.OnClickListener onClickListener;
    private LayoutInflater layoutInflater;


    public HistoryDetailAdapter(Context context) {
        mContext = context;
        layoutInflater = LayoutInflater.from(mContext);

    }

    public void setData(List<HistoryTaskDetail> datas) {
        dataSet = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.item_history_detail, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final HistoryTaskDetail historyTaskList = dataSet.get(position);
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        viewHolder.tvDirection.setText(historyTaskList.getFloor() + "楼  " + historyTaskList.getPosition() + "方向");
        viewHolder.tvParts.setText(historyTaskList.getProblemType());
        viewHolder.tvDescribe.setText(historyTaskList.getProblemOther());
        if (historyTaskList.getImageList() != null && !historyTaskList.getImageList().isEmpty()) {
            for (int k = 0; k < historyTaskList.getImageList().size(); k++) {
                View view = layoutInflater.inflate(R.layout.item_history_detail_img, null);
                viewHolder.flexboxLayout.addView(view);
                ImageView imageView = (ImageView) view.findViewById(R.id.img);
                Picasso.with(mContext)
                        .load(historyTaskList.getImageList().get(k))
                        .error(R.drawable.default_error)
                        .into(imageView);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, ShowBigImage.class);
                        intent.putStringArrayListExtra("data", (ArrayList<String>) historyTaskList.getImageList());
                        mContext.startActivity(intent);
                    }
                });
            }
        }

        if (!TextUtils.isEmpty(historyTaskList.getVideo())) {
            View view = layoutInflater.inflate(R.layout.record_layout, null);
            TextView recordTime = (TextView) view.findViewById(R.id.record_time);
            LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout_play_record);
            recordTime.setText(new StringBuilder().append(historyTaskList.getVideoTime()).append("\"").toString());
            ViewGroup.LayoutParams params = view.getLayoutParams();
            if (params instanceof FlexboxLayout.LayoutParams) {
                FlexboxLayout.LayoutParams layoutParams = (FlexboxLayout.LayoutParams) params;
                layoutParams.setFlexGrow(0);
                layoutParams.width = PixUtils.dip2px(mContext, 90);
                layoutParams.setMaxWidth(PixUtils.dip2px(mContext, 90));
                view.setLayoutParams(layoutParams);
            }
            layout.setOnClickListener(onClickListener);
            layout.setTag(historyTaskList);
            viewHolder.flexboxLayout.addView(view);
        }

        /*HistoryDetailImageAdapter adapter=new HistoryDetailImageAdapter(mContext);
        adapter.setData(historyTaskList.getImageList());
        GridLayoutManager layoutManager=new GridLayoutManager(mContext,4);
        viewHolder.recyclerView.setLayoutManager(layoutManager);
        viewHolder.recyclerView.setAdapter(adapter);*/
    }

    @Override
    public int getItemCount() {
        return dataSet == null ? 0 : dataSet.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_direction)
        TextView tvDirection;
        @Bind(R.id.tv_parts)
        TextView tvParts;
        @Bind(R.id.tv_describe)
        TextView tvDescribe;
        @Bind(R.id.recycler_view)
        RecyclerView recyclerView;
        @Bind(R.id.select_imgs_layout)
        FlexboxLayout flexboxLayout;

        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
